//
//  ZZCircleTool.m
//  DrawLine
//
//  Created by evan on 2017/9/19.
//  Copyright © 2017年 evan. All rights reserved.
//

#import "ZZCircleTool.h"
#import "ZZView.h"
@implementation ZZCircleTool

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    
    static ZZCircleTool *tool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        tool = [super allocWithZone:zone];
    });
    return tool;
}

/**
 返回一个圆形方向渐变的View，颜色渲染方向为顺时针
 */
+ (ZZView *)viewForColors:(NSArray *)colors viewFrame:(CGRect)frame {
    
    if (colors.count < 1) {
        
        return nil;
    }
    
    ZZView *view = [ZZView new];
    //    view.backgroundColor = [UIColor clearColor];
    view.frame = frame;
    //    view.center = CGPointMake(frame.origin.x + frame.size.width / 2, frame.origin.y + frame.size.height / 2);
    //    view.bounds = frame;
    // 额外加10的宽度
    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2 ;
    
    NSMutableArray *mtbColors = [NSMutableArray array];
    
    NSInteger angle = 0;
    // 一种颜色就没什么好渐变的
    for (NSInteger index = 0; index < colors.count; index++) {
        
        CGFloat loopCount = colors.count;
        NSInteger onceLoopCount = 360.0 / loopCount;
        UIColor *color = colors[index];
        NSArray *colorRGB = [self colorForGGB:color];
        UIColor *toColor = nil;
        
        if (index == colors.count - 1) {
            
            toColor = colors[0];
        } else {
            
            toColor = colors[index + 1];
        }
        
        NSArray *toColorRGB = [self colorForGGB:toColor];
        
        CGFloat red = [colorRGB[0] floatValue];
        CGFloat green = [colorRGB[1] floatValue];
        CGFloat blue = [colorRGB[2] floatValue];
        
        CGFloat toRed = [toColorRGB[0] floatValue];
        CGFloat toGreen = [toColorRGB[1] floatValue];
        CGFloat toBlue = [toColorRGB    [2] floatValue];
        
        CGFloat changeRed = red ;
        CGFloat changeGreen = green ;
        CGFloat changeBlue = blue ;
        CGFloat fenShu = 1.0 / onceLoopCount;
        
        for (NSInteger j = 0; j < onceLoopCount; j++) {
            
            
            changeRed = changeRed + (toRed - red) * fenShu ;
            changeGreen = changeGreen + (toGreen - green) * fenShu;
            changeBlue = changeBlue + (toBlue - blue) * fenShu;
            
            //                CGFloat angle = j;
            
            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.bounds = CGRectMake(0, 0, radius * 2, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            layer.shouldRasterize = YES;
            layer.rasterizationScale = [[UIScreen mainScreen] scale];
            layer.borderWidth = 1.0;
            layer.borderColor = [UIColor clearColor].CGColor;
            [view.layer addSublayer:layer];
            layer.rasterizationScale = [[UIScreen mainScreen] scale];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360* (angle)));
            
            if (angle % 10 == 0) {
                
                [mtbColors addObject:[UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1]];
            }
            angle ++;
        }
        
    }
    
    view.colors = [NSMutableArray arrayWithArray:mtbColors];
    view.backgroundColor = [UIColor clearColor];

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;
    
    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    
    return view;
}

/*
 返回环形渐变色圆环 渐变色的方向是圆弧方向
 
 @param colors 输入的颜色，不能为nil
 @param frame view的Frame
 @param lineWidth 圆环线宽
 @param inColor 内圆颜色，不能为透明
 @return 渐变色圆环
 */
+ (ZZView *)viewForColors:(NSArray *)colors viewFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth inCircleColor:(UIColor *)inColor{
    
    ZZView *view = [ZZView new];
    view.backgroundColor = [UIColor clearColor];
    view.frame = frame;
    
    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2;
    
    NSMutableArray *colorsMtb = [NSMutableArray array];
    
    NSInteger angle = 0;
    
    // 一种颜色就没什么好渐变的
    for (NSInteger index = 0; index < colors.count; index++) {
        
        CGFloat loopCount = colors.count;
        NSInteger onceLoopCount = 360 / loopCount;
        UIColor *color = colors[index];
        NSArray *colorRGB = [self colorForGGB:color];
        UIColor *toColor = nil;
        
        
        if (index == colors.count - 1) {
            
            toColor = colors[0];
        } else {
            
            toColor = colors[index + 1];
        }
        
        NSArray *toColorRGB = [self colorForGGB:toColor];
        
        CGFloat red = [colorRGB[0] floatValue];
        CGFloat green = [colorRGB[1] floatValue];
        CGFloat blue = [colorRGB[2] floatValue];
        
        CGFloat toRed = [toColorRGB[0] floatValue];
        CGFloat toGreen = [toColorRGB[1] floatValue];
        CGFloat toBlue = [toColorRGB    [2] floatValue];
        CGFloat changeRed = red ;
        CGFloat changeGreen = green ;
        CGFloat changeBlue = blue ;
        CGFloat fenShu = 1.0 / onceLoopCount;
        
        for (NSInteger j = 0; j < onceLoopCount; j++) {
            
            changeRed = changeRed + (toRed - red) * fenShu ;
            changeGreen = changeGreen + (toGreen - green) * fenShu;
            changeBlue = changeBlue + (toBlue - blue) * fenShu;
            
            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360* (angle)));
            
            if (angle % 10 == 0) {
                
                [colorsMtb addObject: [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1]];
            }
            angle++;
            
        }
        
    }
    
    view.colors = [NSMutableArray arrayWithArray:colorsMtb];
    
    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];
    
    view.backgroundColor = inColor;

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;
    
    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    
    
    return view;
}

+ (ZZView *)viewForColors:(NSArray  *)colors viewFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth inCircleColor:(UIColor *)inColor tuoWeiColor:(UIColor *)tuoWeiColor tuoWeiLength:(CGFloat)tuoWeiLength {
    
    ZZView *view = [ZZView new];
    view.backgroundColor = [UIColor clearColor];
    view.frame = frame;
    // 全部的颜色
    NSMutableArray *mtbColors = [NSMutableArray array];
    // 输出的颜色
    NSMutableArray *outPutColor = [NSMutableArray array];
    NSInteger angle = 0;
    
    int tuoWeiLengthCount = tuoWeiLength * 10 ;
    for (NSInteger i = 0; i < colors.count ; i++) {
        
        
        [mtbColors addObject:colors[i]];
        for (NSInteger j = 0; j < tuoWeiLengthCount; j++) {
            
            [mtbColors addObject:[UIColor colorWithCGColor:tuoWeiColor.CGColor]];
        }
        
    }
    
    
    colors = [mtbColors copy];
    
    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2;
    
    
    
    // 一种颜色就没什么好渐变的
    for (NSInteger index = 0; index < colors.count; index++) {
        
        CGFloat loopCount = colors.count;
        NSInteger onceLoopCount = 360.0 / loopCount;
        CGFloat loop = 360.0 / loopCount;
        int xiaoshu = (int)(loop * 10) % 10;
        if (xiaoshu != 0) {
            
            onceLoopCount = 360.0 / loopCount + 1;
        }
        
        UIColor *color = colors[index];
        NSArray *colorRGB = [self colorForGGB:color];
        UIColor *toColor = nil;
        
        if (index == colors.count - 1) {
            
            toColor = colors[0];
        } else {
            
            toColor = colors[index + 1];
        }
        
        NSArray *toColorRGB = [self colorForGGB:toColor];
        
        CGFloat red = [colorRGB[0] floatValue];
        CGFloat green = [colorRGB[1] floatValue];
        CGFloat blue = [colorRGB[2] floatValue];
        
        CGFloat toRed = [toColorRGB[0] floatValue];
        CGFloat toGreen = [toColorRGB[1] floatValue];
        CGFloat toBlue = [toColorRGB    [2] floatValue];
        CGFloat changeRed = red ;
        CGFloat changeGreen = green ;
        CGFloat changeBlue = blue ;
        CGFloat fenShu = 1.0 / onceLoopCount;
        
        for (NSInteger j = 0; j < onceLoopCount; j++) {
            
            changeRed = changeRed + (toRed - red) * fenShu ;
            changeGreen = changeGreen + (toGreen - green) * fenShu;
            changeBlue = changeBlue + (toBlue - blue) * fenShu;
            
            
            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360.0* (angle)));
            if (angle %10 == 0) {
                
                [outPutColor addObject:[UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1]];
            }
            
            angle++;
        }
        
    }
    
    view.colors = [NSMutableArray arrayWithArray:outPutColor];
    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];
    
    view.backgroundColor = [UIColor clearColor];

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;
    
    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    
    return view;
}

/**
 返回渐变色圆 圆环方向渐变
 
 @param colors 颜色值，不能为nil
 @param frame View的frame值，建议传入正方形frame，不能为zero
 minColor 中间颜色 color
 inColor 内圆颜色
 length 拖尾长度0.1~1 短变长 如果为0 则为全色
 @return 渐变色圆
 */
+ (ZZView *)viewForColors:(NSArray  *)colors viewFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth inCircleColor:(UIColor *)inColor midColor:(UIColor *)midColor midColorLength:(CGFloat)length {
    
    ZZView *view = [ZZView new];
    view.backgroundColor = [UIColor clearColor];
    view.frame = frame;
    
    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2;
    
    NSMutableArray *outPutColor = [NSMutableArray array];
    
    NSMutableArray *mtbcolors = [NSMutableArray array];
    for (int index = 0; index < colors.count; index++) {
        
        [mtbcolors addObject:colors[index]];
        [mtbcolors addObject:midColor];
    }
    
    NSInteger angle = 0;
    
    CGFloat fenShu = 0;
    
    CGFloat loopCount = 360.0 / colors.count;
    
    for (NSInteger index = 0; index < colors.count; index++) {
        
        if(length != 0){
            fenShu = 1.0 / (loopCount * length);
        } else {
            
            fenShu = loopCount;
        }
        
        UIColor *color = mtbcolors[index * 2];
        NSArray *colorRGB = [self colorForGGB:color];
        UIColor *toColor = nil;
        toColor = mtbcolors[index * 2+1];
        
        NSArray *toColorRGB = [self colorForGGB:toColor];
        
        CGFloat red = [colorRGB[0] floatValue];
        CGFloat green = [colorRGB[1] floatValue];
        CGFloat blue = [colorRGB[2] floatValue];
        
        CGFloat toRed = [toColorRGB[0] floatValue];
        CGFloat toGreen = [toColorRGB[1] floatValue];
        CGFloat toBlue = [toColorRGB[2] floatValue];
        
        CGFloat changeRed = red ;
        CGFloat changeGreen = green ;
        CGFloat changeBlue = blue ;
        
        
        
        for (NSInteger i = 0; i < (loopCount * length ); i++) {
            
            // 一种颜色就没什么好渐变的
            changeRed = changeRed + (toRed - red) * fenShu;
            changeGreen = changeGreen + (toGreen - green) * fenShu;
            changeBlue = changeBlue + (toBlue - blue) * fenShu;
            
            
            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360*angle));
            if (angle % 10 == 0) {
                
                [outPutColor addObject:[UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1]];
            }
            
            angle++;
        }
        
        for (NSInteger i = 0; i < loopCount * (1 - length); i++) {
            
            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360* (angle)));
            if (angle % 10 == 0) {
                
                [outPutColor addObject:[UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1]];
            }
            
            angle++;
            
        }
    }
    
    view.colors = [NSMutableArray arrayWithArray:outPutColor];
    
   
    
    view.backgroundColor = inColor;

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;
    
    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    
    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];
    
    return view;
    
}

+ (ZZView *)moreColorViewForColors:(NSArray *)colors viewFrame:(CGRect)frame lineColor:(UIColor *)lineColor lineWitdh:(CGFloat)lineWidth inColor:(UIColor *)inColor{
    
    
    
    if (colors.count < 1) {
        
        return nil;
    }
    
    ZZView *view = [ZZView new];
    view.frame = frame;
    // 额外加10的宽度
    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2 ;
    CGFloat angle = 0;
    // 一种颜色就没什么好渐变的
    for (NSInteger index = 0; index < 120; index++) {
        
        //当前是多少度
        NSInteger colorIndex = index / 10;
        UIColor *color = nil;
        if (colorIndex < colors.count) {
            
            // 画圆
            color = colors[colorIndex];
        } else {
            
            color = lineColor;
        }
        
        // 三色
        NSArray *toColorRGB = [self colorForGGB:color];
        // 赋值颜色
        CGFloat red = [toColorRGB[0] floatValue];
        CGFloat green = [toColorRGB[1] floatValue];
        CGFloat blue = [toColorRGB[2] floatValue];
        
        // 每次画3条线
        for (int j = 0; j < 3; j++) {
            
            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:red / 255.0 green: green / 255.0 blue: blue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 2);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360* (index + j * 120)));
            angle++;
        }
    }
    
    view.colors = [NSMutableArray arrayWithArray:colors];
    
    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];
    
    view.backgroundColor = [UIColor clearColor];

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;
    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    
    return view;
    
}

#pragma mark - 跳变
+ (ZZView *)tiaobianvViewForColors:(NSArray  *)colors viewFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth inCircleColor:(UIColor *)inColor tuoWeiColor:(UIColor *)tuoWeiColor ledNumber:(NSInteger)ledNumber {

    ZZView *view = [ZZView new];
    view.backgroundColor = [UIColor clearColor];
    view.frame = frame;

//    NSInteger ledNumberOffer = 360 / ledNumber;
    // 输出的颜色
    NSMutableArray *outPutColor = [NSMutableArray array];
    NSInteger angle = 0;

    
    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2;
    

    // 一种颜色就没什么好渐变的
    for (NSInteger index = 0; index < colors.count; index++) {
        
        CGFloat loopCount = colors.count;
        NSInteger onceLoopCount = 360.0 / loopCount;
        CGFloat loop = 360.0 / loopCount;
        int xiaoshu = (int)(loop * 10) % 10;
        if (xiaoshu != 0) {
            
            onceLoopCount = 360.0 / loopCount + 1;
        }
        
        UIColor *color = colors[index];
        NSArray *colorRGB = [self colorForGGB:color];
        UIColor *toColor = nil;
        
        if (index == colors.count - 1) {
            
            toColor = colors[0];
        } else {
            
            toColor = colors[index + 1];
        }
        
        NSArray *toColorRGB = [self colorForGGB:toColor];
        
        CGFloat red = [colorRGB[0] floatValue];
        CGFloat green = [colorRGB[1] floatValue];
        CGFloat blue = [colorRGB[2] floatValue];
        
        CGFloat toRed = [toColorRGB[0] floatValue];
        CGFloat toGreen = [toColorRGB[1] floatValue];
        CGFloat toBlue = [toColorRGB    [2] floatValue];
        CGFloat changeRed = red ;
        CGFloat changeGreen = green ;
        CGFloat changeBlue = blue ;
        CGFloat fenShu = 1.0 / onceLoopCount;
        
        for (NSInteger j = 0; j < onceLoopCount; j++) {
            
            changeRed = changeRed + (toRed - red) * fenShu ;
            changeGreen = changeGreen + (toGreen - green) * fenShu;
            changeBlue = changeBlue + (toBlue - blue) * fenShu;
            
            
            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360.0* (angle)));
//            if (angle % ledNumberOffer == 0) {
//
//                [outPutColor addObject:[UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1]];
//            }

            angle++;
        }
    }

//    if (outPutColor.count > 36) {
//
//        outPutColor =[NSMutableArray arrayWithArray: [outPutColor subarrayWithRange: NSMakeRange(0, 36)]];
//    }

//    view.colors = [NSMutableArray arrayWithArray:outPutColor];
    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];
    
    view.backgroundColor = inColor;

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;
    
    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    
    return view;
}

#pragma mark - 覆盖
+ (ZZView *)fuGaiviewForColors:(NSArray  *)colors viewFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth inCircleColor:(UIColor *)inColor midColor:(UIColor *)midColor ledNumber:(NSInteger)ledNumber {

//    if (ledNumber == 0) {
//
//        ledNumber = 36;
//    }
//
//    NSInteger loopCountC = ledNumber / colors.count;
//    NSInteger yuShuCount = ledNumber % colors.count;
//    NSMutableArray *colorsCount = [NSMutableArray array];
//
//    for(NSInteger index = 0; index < colors.count; index ++) {
//
//        [colorsCount addObject:[NSMutableArray array]];
//
//        for (NSInteger j = 0; j < loopCountC; j ++) {
//
//            [colorsCount[index] addObject:colors[index]];
//        }
//    }
//
//    // 平局分给每个数
//    for (NSInteger y = 0; y < yuShuCount; y++) {
//
//        [colorsCount[y] addObject:colors[y]];
//    }
//
//    NSMutableArray *outputColors = [NSMutableArray array];
//
//    for (NSInteger index = 0; index < colorsCount.count; index ++) {
//
//        for (NSInteger j = 0; j< [colorsCount[index] count]; j++) {
//
//            [outputColors[index] addObject: colorsCount[index][j]];
//        }
//    }

    CGFloat length = 0;
    ZZView *view = [ZZView new];
    view.backgroundColor = inColor;
    view.frame = frame;

    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2;

    NSMutableArray *outPutColor = [NSMutableArray array];

    NSMutableArray *mtbcolors = [NSMutableArray array];
    for (int index = 0; index < colors.count; index++) {

        [mtbcolors addObject:colors[index]];
        [mtbcolors addObject:midColor];
    }

    NSInteger angle = 0;

    CGFloat fenShu = 0;

    CGFloat loopCount = 360.0 / colors.count;

    for (NSInteger index = 0; index < colors.count; index++) {

        if(length != 0){
            fenShu = 1.0 / (loopCount * length);
        } else {

            fenShu = loopCount;
        }

        UIColor *color = mtbcolors[index * 2];
        NSArray *colorRGB = [self colorForGGB:color];
        UIColor *toColor = nil;
        toColor = mtbcolors[index * 2+1];

        CGFloat red = [colorRGB[0] floatValue];
        CGFloat green = [colorRGB[1] floatValue];
        CGFloat blue = [colorRGB[2] floatValue];

        CGFloat changeRed = red ;
        CGFloat changeGreen = green ;
        CGFloat changeBlue = blue ;


        for (NSInteger i = 0; i < loopCount * 1 ; i++) {

            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360* (angle)));
            if (angle % 10 == 0) {

                [outPutColor addObject:[UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1]];
            }

            angle++;

        }
    }

    view.colors = [NSMutableArray arrayWithArray:outPutColor];



    view.backgroundColor = inColor;

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;

    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];

    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];


    return view;
}

#pragma mark - 风火轮
+ (ZZView *)fengHuoLunViewForColors:(NSArray  *)colors viewFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth inCircleColor:(UIColor *)inColor tuoWeiColor:(UIColor *)tuoWeiColor tuoWeiLength:(CGFloat)tuoWeiLength ledNumber:(NSInteger)ledNumber {

    if (ledNumber == 0) {

        ledNumber = 36;
    }

    if (tuoWeiLength == 0) {

        tuoWeiLength = 0.1;
    }

    if (tuoWeiLength == 1) {

        tuoWeiLength = 0.99;
    }



    NSMutableArray *outputColors = [NSMutableArray array];

    NSInteger rgbaCount = ledNumber / colors.count;
    NSInteger lengCount = tuoWeiLength * rgbaCount;
    // 如果length的长度大于rgba的长度
    if (lengCount >= rgbaCount / 2) {

        lengCount = rgbaCount / 2 - 1;
    }

    CGFloat offersAlpha = 1.0 / rgbaCount * 2;
    for (int i = 0 ; i < colors.count; i++) {

        NSArray *rgba = [ZZCircleTool colorForGGB:colors[i]];
        for (NSInteger j = 0; j <  rgbaCount; j++) {


            if (j < rgbaCount / 2) {

                if(j < lengCount) {

                      [outputColors addObject:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
                } else {


                [outputColors addObject:[UIColor colorWithRed:[rgba[0] floatValue] / 255.0 green:[rgba[1] floatValue] / 255.0 blue:[rgba[2] floatValue] / 255.0 alpha: j * offersAlpha]];
                     }
            } else {

                if (rgbaCount - j < lengCount) {

                    [outputColors addObject:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
                } else {
                    [outputColors addObject:[UIColor colorWithRed:[rgba[0] floatValue] / 255.0 green:[rgba[1] floatValue] / 255.0 blue:[rgba[2] floatValue] / 255.0 alpha: 2 - j * offersAlpha]];
                }
            }
        }
    }

    ZZView *view = [ZZView new];
    view.backgroundColor = inColor;
    view.frame = frame;
    // 全部的颜色
    NSMutableArray *mtbColors = [NSMutableArray array];
    // 输出的颜色

    NSInteger angle = 0;

    int tuoWeiLengthCount = tuoWeiLength * 10 ;
    for (NSInteger i = 0; i < colors.count ; i++) {


        [mtbColors addObject:colors[i]];
        for (NSInteger j = 0; j < tuoWeiLengthCount; j++) {

            [mtbColors addObject:[UIColor colorWithCGColor:tuoWeiColor.CGColor]];
        }

    }


    colors = [mtbColors copy];

    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2;



    // 一种颜色就没什么好渐变的
    for (NSInteger index = 0; index < colors.count; index++) {

        CGFloat loopCount = colors.count;
        NSInteger onceLoopCount = 360.0 / loopCount;
        CGFloat loop = 360.0 / loopCount;
        int xiaoshu = (int)(loop * 10) % 10;
        if (xiaoshu != 0) {

            onceLoopCount = 360.0 / loopCount + 1;
        }

        UIColor *color = colors[index];
        NSArray *colorRGB = [self colorForGGB:color];
        UIColor *toColor = nil;

        if (index == colors.count - 1) {

            toColor = colors[0];
        } else {

            toColor = colors[index + 1];
        }

        NSArray *toColorRGB = [self colorForGGB:toColor];

        CGFloat red = [colorRGB[0] floatValue];
        CGFloat green = [colorRGB[1] floatValue];
        CGFloat blue = [colorRGB[2] floatValue];

        CGFloat toRed = [toColorRGB[0] floatValue];
        CGFloat toGreen = [toColorRGB[1] floatValue];
        CGFloat toBlue = [toColorRGB    [2] floatValue];
        CGFloat changeRed = red ;
        CGFloat changeGreen = green ;
        CGFloat changeBlue = blue ;
        CGFloat fenShu = 1.0 / onceLoopCount;

        for (NSInteger j = 0; j < onceLoopCount; j++) {

            changeRed = changeRed + (toRed - red) * fenShu ;
            changeGreen = changeGreen + (toGreen - green) * fenShu;
            changeBlue = changeBlue + (toBlue - blue) * fenShu;


            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360.0* (angle)));


            angle++;
        }

    }

    view.colors = [NSMutableArray arrayWithArray:outputColors];
    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];

    view.backgroundColor = inColor;

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;

    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];

    return view;
}


#pragma mark - 太极
+ (ZZView *)taiJiViewForColors:(NSArray  *)colors viewFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth inCircleColor:(UIColor *)inColor midColor:(UIColor *)midColor midColorLength:(CGFloat)length ledNumber:(NSInteger)ledNumber{

    if (ledNumber == 0) {

        ledNumber = 36;
    }

    if (length == 0) {

        length = 0.1;
    }

    if (length >= 1) {

        length = 0.99;
    }

    
    NSMutableArray *outputColors = [NSMutableArray array];

    NSInteger rgbaCount = ledNumber / colors.count;
    NSInteger lengCount = length * rgbaCount;
    // 如果length的长度大于rgba的长度
    if (lengCount >= rgbaCount) {

        lengCount = rgbaCount - 1;
    }

    CGFloat offersAlpha = 1.0 / rgbaCount;
    for (int i = 0 ; i < colors.count; i++) {

        NSArray *rgba = [ZZCircleTool colorForGGB:colors[i]];
        for (NSInteger j = 0; j <  rgbaCount; j++) {

            if (rgbaCount - j > lengCount) {

                [outputColors addObject:[UIColor colorWithRed:[rgba[0] floatValue] / 255.0 green:[rgba[1] floatValue] / 255.0 blue:[rgba[2] floatValue] / 255.0 alpha: 1 -j * offersAlpha]];
            } else {

                    [outputColors addObject:[UIColor colorWithRed:0  green:0 blue:0 alpha:0]];
            }
        }
    }


    ZZView *view = [ZZView new];
    view.backgroundColor = inColor;
    view.frame = frame;

    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2;



    NSMutableArray *mtbcolors = [NSMutableArray array];
    for (int index = 0; index < colors.count; index++) {

        [mtbcolors addObject:colors[index]];
        [mtbcolors addObject:midColor];
    }

    NSInteger angle = 0;

    CGFloat fenShu = 0;

    CGFloat loopCount = 360.0 / colors.count;

    for (NSInteger index = 0; index < colors.count; index++) {

        if(length != 0){
            fenShu = 1.0 / (loopCount * length);
        } else {

            fenShu = loopCount;
        }

        UIColor *color = mtbcolors[index * 2];
        NSArray *colorRGB = [self colorForGGB:color];
        UIColor *toColor = nil;
        toColor = mtbcolors[index * 2+1];

        NSArray *toColorRGB = [self colorForGGB:toColor];

        CGFloat red = [colorRGB[0] floatValue];
        CGFloat green = [colorRGB[1] floatValue];
        CGFloat blue = [colorRGB[2] floatValue];

        CGFloat toRed = [toColorRGB[0] floatValue];
        CGFloat toGreen = [toColorRGB[1] floatValue];
        CGFloat toBlue = [toColorRGB[2] floatValue];

        CGFloat changeRed = red ;
        CGFloat changeGreen = green ;
        CGFloat changeBlue = blue ;



        for (NSInteger i = 0; i < (loopCount * length ); i++) {

            // 一种颜色就没什么好渐变的
            changeRed = changeRed + (toRed - red) * fenShu;
            changeGreen = changeGreen + (toGreen - green) * fenShu;
            changeBlue = changeBlue + (toBlue - blue) * fenShu;


            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360*angle));


            angle++;
        }

        for (NSInteger i = 0; i < loopCount * (1 - length); i++) {

            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:changeRed / 255.0 green: changeGreen / 255.0 blue: changeBlue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 3);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360* (angle)));

            angle++;

        }
    }

    view.colors = [NSMutableArray arrayWithArray:outputColors];



    view.backgroundColor =inColor;

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;

    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];

    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];

    return view;

}

#pragma mark - 七彩
+ (ZZView *)qiCaiMoreColorViewForColors:(NSArray *)colors viewFrame:(CGRect)frame lineColor:(UIColor *)lineColor lineWitdh:(CGFloat)lineWidth inColor:(UIColor *)inColor ledNumber:(NSInteger)ledNumber{




    if (colors.count < 1) {

        return nil;
    }

    ZZView *view = [ZZView new];

    view.frame = frame;
    // 额外加10的宽度
    CGFloat radius = (frame.size.width > frame.size.height? frame.size.height : frame.size.width) / 2 ;
    CGFloat angle = 0;
    CGFloat drawLoopCount = 360.0 / colors.count;
    if (((int)(drawLoopCount * 10) %10) != 0) {

       drawLoopCount = drawLoopCount + 1;
    }
    // 一种颜色就没什么好渐变的
    for (NSInteger index = 0; index < 120; index++) {

        //当前是多少度
        NSInteger colorIndex = index / 10;
        UIColor *color = nil;
        if (colorIndex < colors.count) {

            // 画圆
            color = colors[colorIndex];
        } else {

            color = lineColor;
        }

        // 三色
        NSArray *toColorRGB = [self colorForGGB:color];
        // 赋值颜色
        CGFloat red = [toColorRGB[0] floatValue];
        CGFloat green = [toColorRGB[1] floatValue];
        CGFloat blue = [toColorRGB[2] floatValue];

        // 每次画3条线
        for (int j = 0; j < 3; j++) {

            CALayer *layer = [CALayer layer];
            layer.allowsEdgeAntialiasing = true;
            layer.backgroundColor = [UIColor colorWithRed:red / 255.0 green: green / 255.0 blue: blue / 255.0 alpha:1].CGColor;
            layer.position = CGPointMake(radius, radius);
            layer.shouldRasterize = YES;
            layer.bounds = CGRectMake(0, 0, radius, 2);
            layer.anchorPoint = CGPointMake(0, 0);
            [view.layer addSublayer:layer];
            layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation((M_PI * 2) / 360* (index + j * 120)));
            angle++;
        }
    }

    

    CALayer *inLayer = [CALayer new];
    inLayer.backgroundColor = inColor.CGColor;
    inLayer.shouldRasterize = YES;
    inLayer.allowsEdgeAntialiasing = true;
    inLayer.position = CGPointMake(radius, radius);
    inLayer.bounds = CGRectMake(0, 0, radius * 2 - lineWidth * 2, radius * 2 - lineWidth * 2);
    inLayer.cornerRadius = radius - lineWidth;
    [view.layer addSublayer:inLayer];

    view.backgroundColor = inColor;

    view.layer.allowsEdgeAntialiasing = true;
    view.layer.masksToBounds = YES;
    view.layer.borderColor = [UIColor clearColor].CGColor;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [[UIScreen mainScreen] scale];

    return view;

}

// 返回颜色RBG值
+ (NSArray *)colorForGGB:(UIColor *)color {
    
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    return @[@(components[0] * 255.0),@(components[1] * 255.0),@(components[2] * 255.0),@(components[4])];
}





@end










